package com.skyworth.web.order.repository;

import com.skyworth.web.order.domain.OrderModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
 * Created by skyworth on 2016/6/23.
 */
public interface SubOrderRepository extends PagingAndSortingRepository<OrderModel, Long> {

    /**
     * 删除没提交的购物车订单
     * @param userId
     */
    @Modifying
    @Query("update OrderModel m set m.state =7 where m.state=3 and m.userId = ?1")
    void deleteOrderCart(String userId);

    @Modifying
    @Query("update OrderModel m set m.state =8 where m.parentId=?1 and m.state=1")
    void deleteSubOrder(long id);

    @Modifying
    @Query("update OrderModel m set m.state =8 where m.parentId=?1 and (m.state=0 or m.state=2)")
    void waiterDeleteSubOrder(long id);

    @Modifying
    @Query("update OrderModel m set m.state =5 where m.parentId=?1 and m.state=0")
    void offlineSubOrderPay(long id);

    /**
     * 获取购物车订单
     * @param userId
     * @return
     */
    @Query("select m from OrderModel m where m.state=3 AND m.userId=?1")
    OrderModel getOrderCart(String userId);

    /**
     * 更新购物车订单状态
     * @param tableId
     * @param userId
     */
    @Modifying
    @Query("update OrderModel m set m.state =2,m.tableId = ?1,m.parentId = ?2 where m.state=3 and m.userId = ?3")
    void updateOrderCart(long tableId,long parentId,String userId);

    /**
     * 获取所有子订单的总价格
     * @param userId
     * @return
     */
    @Query(value="select sum(binary m.price) from ff_order m,ff_parent_order n where m.parent_id=n.id AND m.state=0 AND n.state=0 AND n.user_id=?1",nativeQuery = true)
    float getOrderPrice(String userId);

    /**
     * 更新子订单为已结算状态
     * @param userId
     */
    @Modifying
    @Query("update OrderModel m set m.state =1 where m.state=0 and m.userId = ?1")
    void updateOrderState(String userId);

    /**
     * 批量更新子订单
     * @param orderIds
     */
    @Modifying
    @Query("update OrderModel m set m.state =1 where m.id in ?1")
    void updateOrderStateByIds(List<Long> orderIds);

    /**
     * 根据子订单id获取子订单详情
     * @param subOrderId
     * @return
     */
    @Query(value = "select c.`name` storeName,b.id orderId,b.people people,d.`name` tableName,date_format(b.created_time,'%Y-%m-%d %H:%i:%s') createDate,b.original_price originalPrice,b.price price,b.store_id storeId,b.table_id tableId,b.user_id userId,b.state state from ff_order b left join ff_store c on b.store_id=c.id left join ff_table d on b.table_id=d.id where  b.id=?1",nativeQuery = true)
    Object getSubOrderById(long subOrderId);

    /**
     * 同意或拒绝订单
     * @param id
     * @param state
     */
    @Modifying
    @Query("update OrderModel m set m.state=?2 where m.id=?1")
    void setOrderState(Long id, int state);

    @Query(value = "select sum(binary a.price) price from ff_order a where a.id in ?1",nativeQuery = true)
    float getNoPayPriceByIds(List<Long> id);


}
